package com.shxc.smarttrade.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shxc.smarttrade.MyApplication;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.account.fragment.AccountFragment;
import com.shxc.smarttrade.base.BaseActivity;
import com.shxc.smarttrade.community.fragment.CommunityAndroidFragment;
import com.shxc.smarttrade.position.old.PositionFragment;
import com.shxc.smarttrade.quotation.fragment.QuotationFragment;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

/**
 * 选项卡页面
 */
@ContentView(R.layout.activity_maintab)
public class MainActivity2 extends BaseActivity {

    @ViewInject(R.id.framelayout)
    FrameLayout framelayout;
    @ViewInject(R.id.quotation_imageview)
    ImageView quotation_imageview;
    @ViewInject(R.id.quotation_textview)
    TextView quotation_textview;
    @ViewInject(R.id.position_imageview)
    ImageView position_imageview;
    @ViewInject(R.id.position_textview)
    TextView position_textview;
    @ViewInject(R.id.community_imageview)
    ImageView community_imageview;
    @ViewInject(R.id.community_textview)
    TextView community_textview;
    @ViewInject(R.id.account_imageview)
    ImageView account_imageview;
    @ViewInject(R.id.account_textview)
    TextView account_textview;

    private FragmentTransaction mFragmentTransaction;//fragment事务
    private FragmentManager mFragmentManager;//fragment管理者

    private QuotationFragment quotationFragment;
    private PositionFragment positionFragment;
    private CommunityAndroidFragment communityFragment;
    private AccountFragment accountFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragmentManager = getSupportFragmentManager();//获取到fragment的管理对象
        MyApplication.tag = 1;
        setClick(1);

    }

    @Event({R.id.quotation_view, R.id.position_view, R.id.community_view, R.id.account_view})
    private void onClick(View view) {

        clearState();

        switch (view.getId()) {
            case R.id.quotation_view:
                //行情
                quotation_imageview.setImageResource(R.mipmap.quotation_pro);
                quotation_textview.setTextColor(getResources().getColor(R.color.main_color));
                setClick(1);
                break;
            case R.id.position_view:
                //持仓
                position_imageview.setImageResource(R.mipmap.position_pro);
                position_textview.setTextColor(getResources().getColor(R.color.main_color));
                setClick(2);
                break;
            case R.id.community_view:
                //社区
                community_imageview.setImageResource(R.mipmap.community_pro);
                community_textview.setTextColor(getResources().getColor(R.color.main_color));
                setClick(3);
                break;
            case R.id.account_view:
                //我的
                account_imageview.setImageResource(R.mipmap.account_pro);
                account_textview.setTextColor(getResources().getColor(R.color.main_color));
                setClick(4);
                break;
        }

    }

    private void clearState() {
        quotation_imageview.setImageResource(R.mipmap.quotation);
        quotation_textview.setTextColor(getResources().getColor(R.color.tab_text));
        position_imageview.setImageResource(R.mipmap.position);
        position_textview.setTextColor(getResources().getColor(R.color.tab_text));
        community_imageview.setImageResource(R.mipmap.community);
        community_textview.setTextColor(getResources().getColor(R.color.tab_text));
        account_imageview.setImageResource(R.mipmap.account);
        account_textview.setTextColor(getResources().getColor(R.color.tab_text));
    }

    public void setClick(int position) {
        //开启事务
        mFragmentTransaction = mFragmentManager.beginTransaction();
        //显示之前将所有的fragment都隐藏起来,在去显示我们想要显示的fragment
        hideFragment(mFragmentTransaction);
        switch (position) {
            case 1:
                if (quotationFragment == null) {
                    quotationFragment = new QuotationFragment();
                    mFragmentTransaction.add(R.id.framelayout, quotationFragment);
                } else {
                    mFragmentTransaction.show(quotationFragment);
                }
                fragment = quotationFragment;
                break;
            case 2:
                if (positionFragment == null) {
                    positionFragment = new PositionFragment();
                    mFragmentTransaction.add(R.id.framelayout, positionFragment);
                } else {
                    mFragmentTransaction.show(positionFragment);
                }
                fragment = positionFragment;
                break;
            case 3:
                if (communityFragment == null) {
                    communityFragment = new CommunityAndroidFragment();
                    mFragmentTransaction.add(R.id.framelayout, communityFragment);
                } else {
                    mFragmentTransaction.show(communityFragment);
                }
                fragment = communityFragment;
                break;
            case 4:
                if (accountFragment == null) {
                    accountFragment = new AccountFragment();
                    mFragmentTransaction.add(R.id.framelayout, accountFragment);
                } else {
                    mFragmentTransaction.show(accountFragment);
                }
                fragment = accountFragment;
                break;
        }
        //提交事务
        mFragmentTransaction.commit();
    }

    Fragment fragment;

    /**
     * 用来隐藏fragment的方法
     *
     * @param fragmentTransaction
     */
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        //如果此fragment不为空的话就隐藏起来
        if (quotationFragment != null) {
            fragmentTransaction.hide(quotationFragment);
        }
        if (positionFragment != null) {
            fragmentTransaction.hide(positionFragment);
        }
        if (communityFragment != null) {
            fragmentTransaction.hide(communityFragment);
        }
        if (accountFragment != null) {
            fragmentTransaction.hide(accountFragment);
        }
    }

    //连续两次点击后退出应用
    private long firstTime = 0;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {
                    //如果两次按键时间间隔大于2秒，则不退出
                    Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;
                    //更新firstTime
                    return true;
                } else {
                    //两次按键小于2秒时，退出应用
                    System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

}
